package com.ericsson.enap.modules.monitor.service.impl;

import com.ericsson.enap.modules.topology.entity.CabSwitch;
import com.ericsson.enap.modules.monitor.entity.SwitchInterfaceStatus;
import com.ericsson.enap.modules.monitor.entity.SwitchPortData;
import com.ericsson.enap.modules.monitor.entity.SwitchTotalData;
import com.ericsson.enap.modules.monitor.service.SwitchEquipDataService;
import com.ericsson.common.util.TelnetUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("rj")
public class SwitchDataServiceRJImpl implements SwitchEquipDataService {
    @Override
    public String getAlarm(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public SwitchTotalData getUpTime(TelnetUtils telnetUtils, String command) {
        SwitchTotalData switchTotalData = new SwitchTotalData();
        String result = telnetUtils.sendCommand(command);
        String[] s = result.split("\n");
        switchTotalData.setVersion(s[5].substring(s[5].indexOf(":")+1,s[5].length()));
        switchTotalData.setUptime(s[3].substring(s[3].indexOf(":")+1,s[3].length()).replaceFirst(":","w ").replaceFirst(":","d ").replaceFirst(":","h ").concat("m "));
        return switchTotalData;
    }

    @Override
    public String getVlan(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public void displayPage(TelnetUtils telnetUtils, String command) {
        telnetUtils.sendCommand(command);
    }

    @Override
    public String getIpv4Route(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public String getIpv6Route(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public String getConfiguration(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public String getInterface(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public List<SwitchInterfaceStatus> explainInterfaceStatus(String interfaceMsg, String switchName, Map<String,String> statusMap) {
        List<SwitchInterfaceStatus> switchInterfaceStatusList = new ArrayList<>();
        SwitchInterfaceStatus switchInterfaceStatus;
        String[] temp = interfaceMsg.split("\n");
        int remark = 0;
        for (String s : temp) {
            if (remark != 0) {
                String[] sta = s.replaceFirst(" ","").split(" ");
                if (sta.length > 2) {
                    switchInterfaceStatus = new SwitchInterfaceStatus();
                    switchInterfaceStatus.setSwitchName(switchName);
                    int j = 0;
                    for (String m : sta) {
                        if (!m.equals("")) {
                            if (j <= 2) {
                                switch (j) {
                                    case 0:
                                        switchInterfaceStatusList.add(switchInterfaceStatus);
                                        switchInterfaceStatus.setOriginalInterface(m);
                                        switchInterfaceStatus.setSwitchInterface(m.replace("TenGigabitEthernet","Te").replace("GigabitEthernet","Gi"));
                                        break;
                                    case 1:
                                        if (null!=statusMap&&null!=statusMap.get(m.toUpperCase())&&!"".equals(statusMap.get(m.toUpperCase()))) {
                                            switchInterfaceStatus.setSwitchStatus(Long.parseLong(statusMap.get(m.toUpperCase())));
                                        } else {
                                            switchInterfaceStatus.setSwitchStatus(-1);
                                        }
                                        break;
                                }
                            } else {
                                break;
                            }
                            j++;
                        }
                    }
                }
            }
            if (s.contains("----------------------------------------")) {
                remark = 1;
            }
        }
        return switchInterfaceStatusList;
    }

    @Override
    public List<SwitchPortData> getInterfaceDetails(TelnetUtils telnetUtils, String command, String programDate, CabSwitch cabSwitch,Map<String,SwitchPortData> beforeSwitchPortDataMap) {
        List<SwitchPortData> switchPortDataList=new ArrayList<>();
        SwitchPortData switchPortData;
        String result=telnetUtils.sendCommand(command);
        String[] arg = result.split("\n");
        int remarkD = 0;
        String name="";
        String input="";;
        String output="";;
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < arg.length; i++) {
            if (!arg[i].equals("")&&(arg[i].contains("TenGigabitEthernet") || arg[i].contains("GigabitEthernet"))&&arg[i].contains("=")) {
                remarkD=1;
            }else if(arg[i].contains("=")){
                remarkD=0;
            }else if(arg[i].contains("#")){
                remarkD=1;
            }
            if(remarkD==1&&stringBuilder.length()>0){
                switchPortData=new SwitchPortData();
                switchPortData.setSwitchName(cabSwitch.getName());
                switchPortData.setIp(cabSwitch.getAddress());
                if(beforeSwitchPortDataMap.containsKey(cabSwitch.getName()+":"+name)){
                    SwitchPortData before=beforeSwitchPortDataMap.get(cabSwitch.getName()+":"+name);
                    switchPortData.setOutput((Double.parseDouble(output)-before.getOutputTotal())/15*60/1024/1024);
                    switchPortData.setInput((Double.parseDouble(input)-before.getInputTotal())/15*60/1024/1024);
                }else{
                    switchPortData.setInput(Double.parseDouble(input));
                    switchPortData.setOutput(Double.parseDouble(output));
                }
                switchPortData.setInputTotal(Double.parseDouble(input));
                switchPortData.setOutputTotal(Double.parseDouble(output));
                switchPortData.setPortName(name);
                switchPortData.setProgramTime(programDate);
                switchPortData.setTxt(stringBuilder.toString());
                switchPortDataList.add(switchPortData);
                stringBuilder.setLength(0);
            }
            if(remarkD!=0){
                if(remarkD==1){
                    name=arg[i].replaceAll("\\r","").replaceAll("=","").replace("TenGigabitEthernet ","Te").replace("GigabitEthernet ","Gi");
                }
                if(arg[i].contains("packets input,")){
                    input=arg[i].substring(arg[i].indexOf(",")+1,arg[i].indexOf("bytes")).trim();
                }
                if(arg[i].contains("packets output,")){
                    output=arg[i].substring(arg[i].indexOf(",")+1,arg[i].indexOf("bytes")).trim();
                }
                stringBuilder.append(arg[i]+"\n");
                remarkD++;
            }
        }
        return switchPortDataList;
    }

    @Override
    public void exitEquipment(TelnetUtils telnetUtils, String command) {
        telnetUtils.sendCommand(command);
    }
}
